﻿using QxFramework.Core;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using App.Common;

public class BattleManager : LogicModuleBase,IBattleManager
{
    public BattleData _battleData;
    public override void Init()
    {
        base.Init();
        if (!RegisterData(out _battleData))
        {
        }
    }

    public Mono GetCurrentBattleMono()
    {
        return GameMgr.Get<IMonoManager>().GetMono(_battleData.monos[_battleData.CurrentID]);
    }

    public void CreateABattle()
    {
        _battleData.InBattle = true;
        _battleData.BlockInput = true;
        _battleData.Ending = false;
        UIManager.Instance.Open("BattleUI");
        CoroutineManager.Instance.StartCoroutine(CreateBattle());
    }
    IEnumerator CreateBattle()
    {
        yield return new WaitForSeconds(1);
        RefreshTurn();
    }
    private void RefreshTurn()
    {
        _battleData = new BattleData();
        _battleData.InBattle = true;
        _battleData.monos = new List<string>();
        while (_battleData.monos.Count < GameMgr.Get<IMonoManager>().GetAllMono()._MonoList.Count)
        {
            foreach (string monoName in GameMgr.Get<IMonoManager>().GetAllMono()._MonoList.Keys)
            {
                _battleData.monos.Add(monoName);
            }
        }
        _battleData.CurrentID = 0;
        _battleData.BlockInput = false;
        InitMonoStatus();
    }
    private void InitMonoStatus()
    {
        string MonoName = _battleData.monos[_battleData.CurrentID];
        if (!GameMgr.Get<IMonoManager>().GetAllMono()._MonoList.ContainsKey(MonoName))
        {
            CheckNextID();
        }
        GameMgr.Get<IMonoManager>().GetMono(MonoName).LPData.LPValue = GameMgr.Get<IMonoManager>().GetMono(MonoName).LPData.LPCeiling;
        for (int i = 0; i < GameMgr.Get<IMonoManager>().GetMono(MonoName).SkillList.Count; i++)
        {
            GameMgr.Get<IMonoManager>().GetMono(MonoName).MPData.MPValue = GameMgr.Get<IMonoManager>().GetMono(MonoName).MPData.MPCeiling;
        }
    }
    public bool EnemyEndThisTurnCheck()
    {
        if (!GameMgr.Get<IMonoManager>().GetAllMono()._MonoList.ContainsKey(_battleData.monos[_battleData.CurrentID]))
        {
            return true;
        }
        return GameMgr.Get<IMonoManager>().GetMono(_battleData.monos[_battleData.CurrentID]).LPData.LPValue <= 0 || 
            !GameMgr.Get<IMonoManager>().GetObj(GameMgr.Get<IMonoManager>().GetMono(_battleData.monos[_battleData.CurrentID])).GetComponent<MonoBase>().CDingJudge();
    }

    public void EndThisTurn()
    {
        if (!_battleData.Ending)
        {
            CoroutineManager.Instance.StartCoroutine(EndTurn());
        }
    }
    IEnumerator EndTurn()
    {
        GameMgr.Get<IMonoManager>().GetObj(GameMgr.Get<IMonoManager>().GetMono(_battleData.monos[_battleData.CurrentID])).GetComponent<MonoBase>().BuffArrange();
        _battleData.Ending = true;
        _battleData.BlockInput = true;
        yield return new WaitForSeconds(1);
        CheckNextID();
        _battleData.Ending = false;
        _battleData.BlockInput = false;
        yield return 0;
    }
    private void CheckNextID()
    {
        if (_battleData.CurrentID < _battleData.monos.Count - 1)
        {
            _battleData.CurrentID += 1;
            InitMonoStatus();
        }
        else
        {
            RefreshTurn();
        }
    }

    public void EndABattle()
    {
        _battleData.InBattle = false;
    }
    public BattleData GetBattleData()
    {
        return _battleData;
    }
    public bool BattleMonoLPCheck(Mono mono)
    {
        if (!_battleData.InBattle)
        {
            return true;
        }
        if(mono.LPData.LPValue <= 0)
        {
            return false;
        }
        mono.LPData.LPValue -= Data.Instance.TableAgent.GetInt("Base", "MoveUseLP", "Value");
        GameMgr.Get<ITimeManager>().TimeGoing();
        return true;
    }
}
public class BattleData : GameDataBase
{
    public bool InBattle = false;
    public bool BlockInput = false;
    public bool Ending = false;
    public List<string> monos = new List<string>();
    public int CurrentID;
}

